//
//  OMCouponRuleView.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/17.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMCouponRuleView.h"
@interface OMCouponRuleView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *contentView;
@end

@implementation OMCouponRuleView {
    UITableView *_tableView;
}

- (instancetype)init
{
    self = [super init];
    [self configure];
    return self;
}

UIView *backgroundView = nil;
- (void)configure
{
    self.frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SCREEN_HEIGHT);

    backgroundView = [UIView new];
    backgroundView.backgroundColor = [SM_blackColor colorWithAlphaComponent:.4];
    [self addSubview:backgroundView];
    backgroundView.userInteractionEnabled = YES;
    backgroundView.frame = self.frame;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [backgroundView addGestureRecognizer:tap];
    @weakify(self);
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        @strongify(self);
        [self dismiss];
    }];

    _contentView = [UIView new];
    _contentView.backgroundColor = SM_whiteColor;
    [self addSubview:_contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
    }];

    UILabel *titleView = [UILabel new];
    titleView.font = SM_CFont(34);
    titleView.text = @"规则说明";
    titleView.textColor = SM_blackColor;

    [_contentView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(SM_SIZE(60)));
        make.centerX.equalTo(self.contentView.mas_centerX);
    }];

    UILabel *textLabel = [UILabel new];
    textLabel.font = SM_CFont(30);
    textLabel.text = @"优惠券：达到一定金额可使用，每张订单只能用一张";
    textLabel.textColor = SM_darkGrayColor;
    [_contentView addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(40)));
        make.right.equalTo(@(SM_SIZE(-40)));
        make.top.equalTo(titleView.mas_bottom).offset(SM_SIZE(30));
        make.bottom.equalTo(@(SM_SIZE(-100)));
    }];
}

- (void)showInView:(UIView *)view
{
    [view addSubview:self];
    backgroundView.alpha = 0;
    self.contentView.transform = CGAffineTransformMakeTranslation(0, SM_SIZE(250));
    [UIView animateWithDuration:.2 animations:^{
        backgroundView.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:.2 animations:^{
        backgroundView.alpha = 0;
        [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.top.equalTo(@(SM_SCREEN_HEIGHT));
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

}

@end
